package org.ly.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.ly.config.CustomConfig;
import org.ly.dao.CmdbHost;
import org.ly.dao.CmdbHostRepository;
import org.ly.util.CustomResponse;
import org.ly.util.LogUtil;
import org.ly.util.RequestUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class CmdbHostService {

    private final CmdbHostRepository cmdbHostRepository;

    private final CustomConfig customConfig;

    public CmdbHostService(CmdbHostRepository cmdbHostRepository, CustomConfig customConfig) {
        this.cmdbHostRepository = cmdbHostRepository;
        this.customConfig = customConfig;
    }

    public void cmdb2local() {
        try {
            String cmdbJson;
            String url = customConfig.getUrl();
            Map<String, String> headers = Map.of(
                    "Accept", customConfig.getAccept(),
                    "Accept-Language", customConfig.getAcceptLanguage(),
                    "Auth-Token", customConfig.getAuthToken(),
                    "Connection", customConfig.getConnection(),
                    "Content-Type", customConfig.getContentType(),
                    "Cookie", customConfig.getCookie(),
                    "Origin", customConfig.getOrigin(),
                    "Referer", customConfig.getReferer(),
                    "Route", customConfig.getRoute(),
                    "User-Agent", customConfig.getUserAgent()
            );
            String jsonInputString = customConfig.getJsonInputString();

            CustomResponse customResponse = RequestUtil.sendPostRequest(url, headers, jsonInputString);

            cmdbJson = customResponse.getData();
//            LogUtil.logInfo(cmdbJson);
            List<CmdbHost> cmdbHostList = CmdbHostService.resolveCmdbJson(cmdbJson);
            cmdbHostRepository.saveAll(cmdbHostList); // TODO: 2024/10/24 位置
        } catch (Exception e) {
            LogUtil.logError(e.toString(), e);
        }
    }

    public static List<CmdbHost> resolveCmdbJson(String cmdbJson) {

        List<CmdbHost> cmdbHostList = new ArrayList<>();
        try {
            JSONObject dataObject = JSONObject.parseObject(cmdbJson).getJSONObject("data");
            JSONArray infoObject = dataObject.getJSONArray("info");
            for(int i = 0;i<infoObject.size(); i++) {
                JSONObject hostObject = infoObject.getJSONObject(i).getJSONObject("host");
                CmdbHost cmdbHost;
                cmdbHost = new CmdbHost();
                cmdbHost.setHostInnerip(hostObject.getString("host_innerip"));
                cmdbHost.setHostName(hostObject.getString("host_name"));
                cmdbHost.setHostId(hostObject.getString("Host_id"));
                cmdbHost.setAreaCode(hostObject.getString("area_code"));
                cmdbHost.setHostState(hostObject.getString("host_state"));
                cmdbHost.setArea(hostObject.getString("area"));
                cmdbHost.setResourcePool(hostObject.getString("resource_pool"));
                cmdbHost.setHostType(hostObject.getString("host_type"));
                cmdbHost.setOrganization(hostObject.getString("organization"));
                cmdbHostList.add(cmdbHost);
            }

        } catch (Exception e) {
            LogUtil.logError(e.toString(), e);
        }
        return cmdbHostList;
    }
}
